package com.wang.myblog.controller;

import com.alibaba.fastjson.JSON;
import com.wang.myblog.common.ResponseResult;
import com.wang.myblog.entity.Front;
import com.wang.myblog.entity.Link;
import com.wang.myblog.service.FrontService;
import com.wang.myblog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.bind.util.JAXBSource;
import java.util.List;

@Controller
public class FrontController {

    @Autowired
    private FrontService frontService;

    @Autowired
    private LinkService linkService;

    @RequestMapping("/frontInfo")
    public String frontInfo(Model model){
        List<Link> links=linkService.queryAll();
        Front front=frontService.queryById(1);
        model.addAttribute("front",front);
        model.addAttribute("links",links);
        return "admin/front";
    }

    /**
     * 更新主页信息
     * @param front
     * @return
     */
    @RequestMapping("/setFrontInfo")
    @ResponseBody
    public String setFrontInfo(@RequestBody Front front){
        front.setId(1);
        System.out.println(front);
        ResponseResult result=new ResponseResult();
        if(frontService.update(front)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("更新失败");
        }
        return JSON.toJSONString(result);
    }

    /**
     * 新增友情链接
     */
    @RequestMapping("/addLink")
    @ResponseBody
    public String addLink(@RequestBody Link link){
        ResponseResult result=new ResponseResult();
        if(linkService.insert(link)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("添加失败");
        }
        return JSON.toJSONString(result);
    }

    /**
     * 删除友情链接
     */
    @RequestMapping("/deleteLink")
    @ResponseBody
    public String delete(Integer id){
        ResponseResult result=new ResponseResult();
        if(linkService.deleteById(id)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("删除失败");
        }
        return JSON.toJSONString(result);
    }

    /**
     * 前往编辑链接界面
     */
    @RequestMapping("/editLink")
    public String editLink(Integer id,Model model){
        Link link=linkService.queryById(id);
        model.addAttribute("link",link);
        return "admin/editLink";
    }

    /**
     * 编辑友情链接
     */
    @RequestMapping("/doEditLink")
    @ResponseBody
    public String doEditLink(@RequestBody Link link){
        ResponseResult result=new ResponseResult();
        if(linkService.update(link)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("更新链接失败");
        }
        return JSON.toJSONString(result);
    }



}
